# ----------------------------------------------------------------------
#  Project: "Prosocial Behavior amid Violence -- Study 2"
#  Journal: Political Psychology
#  Last updated: "Aug 13 2023"
# ----------------------------------------------------------------------


rm(list = ls())

# set wd
setwd("")

# Loading Necessary Libraries ----
library(tidyverse)
library(coefplot)
library(kableExtra)
library(modelsummary)
library(cjoint)
source("plot_amce.R")

d4 <- readRDS("data_study2.rds")

# rename Extortion (previously labeled "Extorsion")
levels(d4$Crime) <- c("Disappeared", "Displaced" , "Extortion" ,  "Murder",      "Torture" )

# main outcomes standarized
d4 <- d4 %>%
  rename(`Nature of Violence` = `Type of Violence`) %>%
  mutate(z_donatedummy = scale(donatedummy),
         z_donateamount = scale(donateamount),
         z_therm = scale(therm),
         z_socialdist = scale(socialdist),
         # solidarity index
         z_outcomes = stdidx::idx_mean(z_donatedummy, z_donateamount,
                                       z_therm, z_socialdist))

#### Main analysis ----
#### Fig. A1
## Standarized outcomes, per PAP
d5 <- d4[!(is.na(d4$z_outcomes)),]
d5 <- d5 %>%
  dplyr::filter(!is.na(`Respondent Ethnicity`))
m = amce(z_outcomes ~  `Nature of Violence` + Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot.amce(m, ci=.95, xlab="Change in Solidarity Index",
          text.size=16, point.size = 1,
          group.order = c("Nature of Violence", "Crime", "Perpetrator",
                          "Victim Occupation",  "Victim Nationality",
                          "Victim Ethnicity", "Victim Gender",
                          "Respondent Ethnicity", "Respondent Gender",
                          "Respondent SES"))
ggsave(file="fig_solidarity_index.pdf", width = 11, height = 12.5, dpi = 600)

##### Fig. 2a
## Donate dummy
d5 <- d4[!(is.na(d4$donatedummy)),]
d5 <- d5 %>%
  dplyr::filter(!is.na(`Respondent Ethnicity`))
d5$donatedummy = as.numeric(d5$donatedummy)
m = amce(donatedummy ~  `Nature of Violence` + Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot.amce(m, ci=.95, xlab="Change in Pr(Donating)",
          text.size=16, point.size = 1,
     group.order = c("Nature of Violence", "Crime", "Perpetrator",
                     "Victim Occupation",  "Victim Nationality",
                     "Victim Ethnicity", "Victim Gender",
                     "Respondent Ethnicity", "Respondent Gender",
                     "Respondent SES"))
ggsave(file="fig_donatedummy.pdf", width = 11, height = 12.5, dpi = 600)

##### Fig. 2b
## Donate amount
d5 <- d4[!(is.na(d4$donateamount)),]
d5 <- d4[!(is.na(d4$`Respondent Ethnicity`)),]
m = amce(donateamount ~  `Nature of Violence` + Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "ticket")
plot.amce(m, ci=.95, xlab="Change in Amount Donated", text.size=16,
          point.size = 1,
          group.order = c("Nature of Violence", "Crime", "Perpetrator",
                          "Victim Occupation",  "Victim Nationality",
                          "Victim Ethnicity", "Victim Gender",
                          "Respondent Ethnicity", "Respondent Gender",
                          "Respondent SES"))
ggsave(file="fig_donateamount.pdf", width=11, height=12.5, dpi=600)

#### Fig. 3a
## Feeling thermometer
d5 <- d4[!(is.na(d4$therm)),]
d5 <- d4[!(is.na(d4$`Respondent Ethnicity`)),]
m = amce(therm ~  `Nature of Violence` + Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot.amce(m, ci=.95,
          xlab="Change in Feelings Toward Victim (Feeling Thermometer)",
          text.size=16, point.size = 1,
          group.order = c("Nature of Violence", "Crime", "Perpetrator",
                          "Victim Occupation",  "Victim Nationality",
                          "Victim Ethnicity", "Victim Gender",
                          "Respondent Ethnicity", "Respondent Gender",
                          "Respondent SES"))
ggsave(file="fig_therm.pdf", width=11, height=12.5, dpi=600)


#### Fig. 3b
# Social Distance
d5 <- d4[!(is.na(d4$socialdist)),]
d5 <- d4 %>%
  dplyr::filter(!is.na(socialdist), !is.na(`Respondent Ethnicity`))
m = amce(socialdist ~  `Nature of Violence` + Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot(m, ci=.95, xlab="Change in Social Distance Tolerance with Victims",
     text.size=16, point.size = 1,
     group.order = c("Nature of Violence", "Crime", "Perpetrator",
                     "Victim Occupation",  "Victim Nationality",
                     "Victim Ethnicity", "Victim Gender",
                     "Respondent Ethnicity", "Respondent Gender",
                     "Respondent SES"))
ggsave(file="fig_socialdistance.pdf", width=11, height=12.5, dpi=600)


# Run as OLS models to export table (classic packages don't support AMCE)
## Donate dummy
d5 <- d4[!(is.na(d4$donatedummy)),]
d5 <- d4[!(is.na(d4$`Respondent Ethnicity`)),]
d5$donatedummy = as.numeric(d5$donatedummy)
m.donatedummy = lm_robust(donatedummy ~  `Nature of Violence` + Perpetrator +
                     Crime + `Victim Occupation` +
                     `Victim Nationality` + `Victim Ethnicity` +
                     `Victim Gender` + `Respondent Ethnicity` +
                     `Respondent Gender`+ `Respondent SES`, data = d5,
                     clusters = resp.id, se_type = "stata")

## Donate amount
d5 <- d4[!(is.na(d4$donateamount)),]
m.donateamount = lm_robust(donateamount ~  `Nature of Violence` + Perpetrator +
                      Crime + `Victim Occupation` +
                      `Victim Nationality` + `Victim Ethnicity` +
                      `Victim Gender` + `Respondent Ethnicity` +
                      `Respondent Gender`+ `Respondent SES`, data = d5,
                      clusters = resp.id, se_type = "stata")

## Feeling thermometer
d5 <- d4[!(is.na(d4$therm)),]
m.therm = lm_robust(therm ~  `Nature of Violence` + Perpetrator +
               Crime + `Victim Occupation` +
               `Victim Nationality` + `Victim Ethnicity` +
               `Victim Gender` + `Respondent Ethnicity` +
               `Respondent Gender`+ `Respondent SES`, data = d5,
               clusters = resp.id, se_type = "stata")

# Social Distance
d5 <- d4[!(is.na(d4$socialdist)),]
d5 <- d4 %>%
  dplyr::filter(!is.na(socialdist), !is.na(`Respondent Ethnicity`))
m.socialdist = lm_robust(socialdist ~  `Nature of Violence` + Perpetrator +
                    Crime + `Victim Occupation` +
                    `Victim Nationality` + `Victim Ethnicity` +
                    `Victim Gender` + `Respondent Ethnicity` +
                    `Respondent Gender`+ `Respondent SES`, data = d5,
                    clusters = resp.id, se_type = "stata")

# Empathy/Compassion
d5 <- d4 %>%
  dplyr::filter(!is.na(empathy), !is.na(`Respondent Ethnicity`))
m.empathy = lm_robust(empathy ~  `Nature of Violence` + Perpetrator +
                 Crime + `Victim Occupation` +
                 `Victim Nationality` + `Victim Ethnicity` +
                 `Victim Gender` + `Respondent Ethnicity` +
                 `Respondent Gender`+ `Respondent SES`, data = d5,
                 clusters = resp.id, se_type = "stata")


d5 <- d4 %>%
  dplyr::filter(!is.na(norms), !is.na(`Respondent Ethnicity`))
m.norms = lm_robust(norms ~  `Nature of Violence` + Perpetrator +
               Crime + `Victim Occupation` +
               `Victim Nationality` + `Victim Ethnicity` +
               `Victim Gender` + `Respondent Ethnicity` +
               `Respondent Gender`+ `Respondent SES`, data = d5,
               clusters = resp.id, se_type = "stata")

## export table
outcome_models <- list(
    "Donation (Dummy)" = m.donatedummy,
    "Donation (Amount)" = m.donateamount,
    "Feeling Thermometer" = m.therm,
    "Social Distance" = m.socialdist
    )

#### Table A5
msummary(outcome_models, stars = T, coef_omit = "Intercept",
         coef_rename = c("`Nature of Violence`Indiscriminate" = "Indiscriminate Violence",
                         "PerpetratorNarcos" = "Peroetrator: Narcos",
                         "PerpetratorPolice" = "Perpetrator: Police",
                         "CrimeDisplaced" = "Crime: Displacement",
                         "CrimeExtorsion" = "Crime: Extortion",
                         "CrimeMurder" = "Crime: Murder",
                         "CrimeTorture" = "Crime: Torture",
                         "`Victim Occupation`Street Vendor" = "Victim Occupation: Street Vendor",
                         "`Victim Nationality`Mexican" = "Victim Nationality: Mexican",
                         "`Victim Ethnicity`Mixed" = "Victim Ethnicity: Mixed",
                         "`Victim Ethnicity`White" = "Victim Ethnicity: White",
                         "`Victim Gender`Male" = "Victim Gender: Male",
                         "`Respondent Ethnicity`Same as Victim" =
                           "Respondent Ethnicity: Same as Victim",
                         "`Respondent Gender`Same as Victim" = 
                           "Respondent Gender: Same as Victim",
                         "`Respondent SES`Same as Victim" = 
                           "Respondent SES: Same as Victim"),
         title = "Factorial experiment tabular results (outcomes)",
         output = "latex",
         notes = "Standard error clustered by respondent.")


mech_models <- list(
  "Compassion" = m.empathy,
  "Social Norms" = m.norms
)

#### Table A6
msummary(mech_models, stars = T, coef_omit = "Intercept",
         coef_rename = c("`Nature of Violence`Indiscriminate" = "Indiscriminate Violence",
                         "PerpetratorNarcos" = "Peroetrator: Narcos",
                         "PerpetratorPolice" = "Perpetrator: Police",
                         "CrimeDisplaced" = "Crime: Displacement",
                         "CrimeExtorsion" = "Crime:  Extortion",
                         "CrimeMurder" = "Crime: Murder",
                         "CrimeTorture" = "Crime: Torture",
                         "`Victim Occupation`Street Vendor" = "Victim Occupation: Street Vendor",
                         "`Victim Nationality`Mexican" = "Victim Nationality: Mexican",
                         "`Victim Ethnicity`Mixed" = "Victim Ethnicity: Mixed",
                         "`Victim Ethnicity`White" = "Victim Ethnicity: White",
                         "`Victim Gender`Male" = "Victim Gender: Male",
                         "`Respondent Ethnicity`Same as Victim" =
                           "Respondent Ethnicity: Same as Victim",
                         "`Respondent Gender`Same as Victim" = 
                           "Respondent Gender: Same as Victim",
                         "`Respondent SES`Same as Victim" = 
                           "Respondent SES: Same as Victim"),
         title = "Factorial experiment tabular results (Mechanisms)",
         output = "latex",
         notes = "Standard error clustered by respondent.")



####### Mechanisms as DV  -------
#### Fig. 4a
# Empathy/Compassion
d5 <- d4 %>%
  dplyr::filter(!is.na(empathy), !is.na(`Respondent Ethnicity`))
m = amce(empathy ~  `Nature of Violence` + Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot.amce(m, ci=.95, xlab="Change in Compassion for Victims",
     text.size=16, point.size = 1,
     group.order = c("Nature of Violence", "Crime", "Perpetrator",
                     "Victim Occupation",  "Victim Nationality",
                     "Victim Ethnicity", "Victim Gender",
                     "Respondent Ethnicity", "Respondent Gender",
                     "Respondent SES"))
ggsave(file="fig_empathy.pdf", width=11, height=12.5, dpi=600)

#### Fig. 4b
# Social Norms
d5 <- d4 %>%
  dplyr::filter(!is.na(norms), !is.na(`Respondent Ethnicity`))
m = amce(norms ~  `Nature of Violence` + Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot(m, ci=.95, xlab="Change in Social Norms about Helping Victims",
     text.size=16, point.size = 1,
     group.order = c("Nature of Violence", "Crime", "Perpetrator",
                     "Victim Occupation",  "Victim Nationality",
                     "Victim Ethnicity", "Victim Gender",
                     "Respondent Ethnicity", "Respondent Gender",
                     "Respondent SES"))
ggsave(file="fig_norms.pdf", width=11, height=12.5, dpi=600)


####### Mechanisms with mediation
library(mediation)
set.seed(301190)

#### Table 4 & Fig. A2 & A3
### Mechanism 1: Empathy
# DV1: Donate dummy
d5 <- d4[!(is.na(d4$donatedummy)),]
med.fit <- lm(empathy ~  `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
out.fit <- lm(donatedummy ~ empathy + `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
donatedummy_empathy_med.out <- mediate(med.fit, out.fit, treat = "Nature of Violence",
                    mediator = "empathy",
                    robustSE = TRUE, sims = 1000); summary(donatedummy_empathy_med.out);plot(donatedummy_empathy_med.out)
# sensitivity
donatedummy_empathy_sens.out <- medsens(donatedummy_empathy_med.out, rho.by = 0.1, effect.type = "indirect", sims = 100); summary(donatedummy_empathy_sens.out)

# DV2: Donate amount
d5 <- d4[!(is.na(d4$donateamount)),]
med.fit <- lm(empathy ~  `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
out.fit <- lm(donateamount ~ empathy + `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
donateamount_empathy_med.out <- mediate(med.fit, out.fit, treat = "Nature of Violence",
                   mediator = "empathy",
                   robustSE = TRUE, sims = 1000); summary(donateamount_empathy_med.out);plot(donateamount_empathy_med.out)

# DV3: FT
d5 <- d4[!(is.na(d4$therm)),]
med.fit <- lm(empathy ~  `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
out.fit <- lm(therm ~ empathy + `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
ft_empathy_med.out <- mediate(med.fit, out.fit, treat = "Nature of Violence",
                   mediator = "empathy",
                   robustSE = TRUE, sims = 1000); summary(ft_empathy_med.out);plot(ft_empathy_med.out)

# DV4: Social distance
d5 <- d4[!(is.na(d4$socialdist)),]
med.fit <- lm(empathy ~  `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
out.fit <- lm(socialdist ~ empathy + `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
sd_empathy_med.out <- mediate(med.fit, out.fit, treat = "Nature of Violence",
                   mediator = "empathy",
                   robustSE = TRUE, sims = 1000); summary(sd_empathy_med.out);plot(sd_empathy_med.out)


### Mechanism 2: Social norms
# DV1: Donate dummy
d5 <- d4[!(is.na(d4$donatedummy)),]
med.fit <- lm(norms ~  `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
out.fit <- lm(donatedummy ~ norms + `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
donatedummy_norms_med.out <- mediate(med.fit, out.fit, treat = "Nature of Violence",
                   mediator = "norms",
                   robustSE = TRUE, sims = 1000); summary(donatedummy_norms_med.out);plot(donatedummy_norms_med.out)

# DV2: Donate amount
d5 <- d4[!(is.na(d4$donateamount)),]
med.fit <- lm(norms ~  `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
out.fit <- lm(donateamount ~ norms + `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
donateamount_norms_med.out <- mediate(med.fit, out.fit, treat = "Nature of Violence",
                   mediator = "norms",
                   robustSE = TRUE, sims = 1000); summary(donateamount_norms_med.out);plot(donateamount_norms_med.out)

# DV3: FT
d5 <- d4[!(is.na(d4$therm)),]
med.fit <- lm(norms ~  `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
out.fit <- lm(therm ~ norms + `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
ft_norms_med.out <- mediate(med.fit, out.fit, treat = "Nature of Violence",
                   mediator = "norms",
                   robustSE = TRUE, sims = 1000); summary(ft_norms_med.out);plot(ft_norms_med.out)

# DV4: Social distance
d5 <- d4[!(is.na(d4$socialdist)),]
med.fit <- lm(norms ~  `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
out.fit <- lm(socialdist ~ norms + `Nature of Violence` + Perpetrator + Crime +
                `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                `Victim Gender` +
                `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
              data = d5)
sd_norms_med.out <- mediate(med.fit, out.fit, treat = "Nature of Violence",
                   mediator = "norms",
                   robustSE = TRUE, sims = 1000); summary(sd_norms_med.out);plot(sd_norms_med.out)

# Create a list of the med.out models
model_list <- list( # compassion mechanism 4 models
                   donatedummy_empathy_med.out, donateamount_empathy_med.out,
                   ft_empathy_med.out, sd_empathy_med.out,
                   # norms mechanism 4 models
                   donatedummy_norms_med.out, donateamount_norms_med.out,
                   ft_norms_med.out, sd_norms_med.out)

names(model_list) <-  c("Donation (Dummy)", "Donation (Amount)",
                        "Feeling\nThermometer", "Social\nDistance",
                        "Donation (Dummy)", "Donation (Amount)",
                        "Feeling\nThermometer", "Social\nDistance")


# Export the models in a latex table
msummary(model_list, 
         coef_map = c("acme_0" = "Indiscriminate Violence"),
         coef_omit = 'Intercept',
         stars = T,
         title = "Average Causal Mediation Effects of Nature of Violence on Behaviors and Attitudes toward Victims")  %>%
  add_header_above(c(" " = 1, "Mechanism: Compassion" = 4,
                     "Mechanism: Social Norms" = 4))

####################### APPENDIX #######################

## Heterogenous Effects with cregg ----
library(cregg)
## Conservatives vs. Liberals
d4 <- d4 %>%
  dplyr::mutate(Ideology = ifelse(d4$ideology_1 > 5, 1, 0),
                Ideology = factor(Ideology,
                                  levels = c(0, 1),
                                  labels = c("Liberal", "Conservative")))
d5 <- d4[!(is.na(d4$donatedummy)),]
conservative_mm_ftest <- cj_anova(donatedummy ~  
                                    `Nature of Violence` + Perpetrator + Crime +
                                    `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
                                    `Victim Gender` + `Respondent Ethnicity` +
                                    `Respondent Gender`+ `Respondent SES`,
                                  data = d5, id = ~ ticket, by = ~ Ideology)
conservative_mm_ftest

# donatedummy
conservative_mm <- cj(donatedummy ~ 
                        `Nature of Violence` + Perpetrator + Crime +
                        `Victim Occupation` + `Victim Nationality` + 
                        `Victim Ethnicity` +
                        `Victim Gender` + `Respondent Ethnicity`,
                      data = d5, id = ~ ticket, by = ~ Ideology, h0=0.5,
                      estimate="mm")
plot(conservative_mm, group="Ideology", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("Conservative_mm_donatedummy.pdf", width = 11, height = 12.5, dpi = 600)

# donate (amount)
conservative_mm_donateamount <- cj(donateamount ~ 
                                     `Nature of Violence` + Perpetrator + Crime +
                                     `Victim Occupation` + `Victim Nationality` + 
                                     `Victim Ethnicity` +
                                     `Victim Gender` + `Respondent Ethnicity`,
                                   data = d5, id = ~ ticket, by = ~ Ideology,
                                   h0=0.5, estimate="mm")
plot(conservative_mm_donateamount, group="Ideology", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("Conservative_mm_donateamount.pdf",
       width = 11, height = 12.5, dpi = 600)

# feeling therm
conservative_therm_mm <- cj(therm ~ 
                              `Nature of Violence` + Perpetrator + Crime +
                              `Victim Occupation` + `Victim Nationality` + 
                              `Victim Ethnicity` +
                              `Victim Gender` + `Respondent Ethnicity`,
                            data = d5, id = ~ ticket, by = ~ Ideology, h0=0.5,
                            estimate="mm")
plot(conservative_therm_mm, group="Ideology", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("Conservative_mm_therm.pdf", width = 11, height = 12.5, dpi = 600)


# social distance
conservative_socdist_mm <- cj(socialdist ~ 
                                `Nature of Violence` + Perpetrator + Crime +
                                `Victim Occupation` + `Victim Nationality` + 
                                `Victim Ethnicity` +
                                `Victim Gender` + `Respondent Ethnicity`,
                              data = d5, id = ~ ticket, by = ~ Ideology, h0=0.5,
                              estimate="mm")
plot(conservative_socdist_mm, group="Ideology", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("Conservative_mm_socialdistance.pdf",
       width = 11, height = 12.5, dpi = 600)


# Empathy
conservative_empathy_mm <- cj(empathy ~ 
                                `Nature of Violence` + Perpetrator + Crime +
                                `Victim Occupation` + `Victim Nationality` + 
                                `Victim Ethnicity` +
                                `Victim Gender` + `Respondent Ethnicity`,
                              data = d5, id = ~ ticket, by = ~ Ideology, 
                              h0=0.5, estimate="mm")
plot(conservative_empathy_mm, group="Ideology", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("Conservative_mm_empathy.pdf", width = 11, height = 12.5, dpi = 600)

# Norms
conservative_norms_mm <- cj(norms ~ 
                              `Nature of Violence` + Perpetrator + Crime +
                              `Victim Occupation` + `Victim Nationality` + 
                              `Victim Ethnicity` +
                              `Victim Gender` + `Respondent Ethnicity`,
                            data = d5, id = ~ ticket, by = ~ Ideology, 
                            h0=0.5, estimate="mm")
plot(conservative_norms_mm, group = "Ideology", vline = .5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("Conservative_mm_norms.pdf", width = 11, height = 12.5, dpi = 600)

## Victims vs. Non-Victims
# donatedummy
d5 <- d4 %>%
  dplyr::mutate(`Indirect Victim` = factor(
    `Indirect Victim`,
    levels = c(0, 1),
    labels = c("No", "Yes"))) %>%
  dplyr::filter(!is.na(d4$donatedummy)) %>%
  dplyr::rename(Victimized = `Indirect Victim`)
victimization_mm <- cj(donatedummy ~ 
                         `Nature of Violence` + Perpetrator + Crime +
                         `Victim Occupation` + `Victim Nationality` + 
                         `Victim Ethnicity` +
                         `Victim Gender` + `Respondent Ethnicity`,
                       data = d5, id = ~ ticket, by = ~ Victimized, 
                       h0 = 0.5, estimate = "mm")
plot(victimization_mm, group="Victimized", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave(file="Victimization_mm_donatedummy.pdf",
       width = 11, height = 12.5, dpi = 600)

# donate (amount)
victimization_mm_donateamount <- cj(donateamount ~ 
                                      `Nature of Violence` + Perpetrator + Crime +
                                      `Victim Occupation` + `Victim Nationality` + 
                                      `Victim Ethnicity` +
                                      `Victim Gender` + `Respondent Ethnicity`,
                                    data = d5, id = ~ ticket, by = ~ Victimized, h0=0.5,
                                    estimate="mm")
plot(victimization_mm_donateamount, group = "Victimized", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("Victimization_mm_donateamount.pdf",
       width = 11, height = 12.5, dpi = 600)

# feeling therm
victimization_therm_mm <- cj(therm ~ 
                               `Nature of Violence` + Perpetrator + Crime +
                               `Victim Occupation` + `Victim Nationality` + 
                               `Victim Ethnicity` +
                               `Victim Gender` + `Respondent Ethnicity`,
                             data = d5, id = ~ ticket, by = ~ Victimized, 
                             h0 = 0.5, estimate = "mm")
plot(victimization_therm_mm, group = "Victimized", vline = .5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave(file="Victimization_mm_therm.pdf",
       width = 11, height = 12.5, dpi = 600)

# social distance
victimization_socialdist_mm <- cj(socialdist ~ 
                                    `Nature of Violence` + Perpetrator + Crime +
                                    `Victim Occupation` + `Victim Nationality` + 
                                    `Victim Ethnicity` +
                                    `Victim Gender` + `Respondent Ethnicity`,
                                  data = d5, id = ~ ticket, by = ~ Victimized, 
                                  h0 = 0.5, estimate = "mm")
plot(victimization_socialdist_mm, group = "Victimized", vline = .5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave(file="Victimization_mm_socialdistance.pdf",
       width = 11, height = 12.5, dpi = 600)

# Empathy
victimization_empathy_mm <- cj(empathy ~ 
                                 `Nature of Violence` + Perpetrator + Crime +
                                 `Victim Occupation` + `Victim Nationality` + 
                                 `Victim Ethnicity` +
                                 `Victim Gender` + `Respondent Ethnicity`,
                               data = d5, id = ~ ticket, by = ~ Victimized, 
                               h0=0.5, estimate="mm")
plot(victimization_empathy_mm, group = "Victimized", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave(file="Victimization_mm_empathy.pdf",
       width = 11, height = 12.5, dpi = 600)

# Norms
victimization_norms_mm <- cj(norms ~ 
                               `Nature of Violence` + Perpetrator + Crime +
                               `Victim Occupation` + `Victim Nationality` + 
                               `Victim Ethnicity` +
                               `Victim Gender` + `Respondent Ethnicity`,
                             data = d5, id = ~ ticket, by = ~ Victimized, 
                             h0=0.5, estimate="mm")
plot(victimization_norms_mm, group = "Victimized", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave(file="Victimization_mm_norms.pdf",
       width = 11, height = 12.5, dpi = 600)




## By Level of Education
d4 <- d4 %>%
  # educ = 7 is 'Preparatoria Terminada', so Education = 1 are those who are at
  # least ready to start college
  dplyr::mutate(Education = ifelse(d4$educ >= 7, 1, 0),
                Education = factor(Education,
                                   levels = c(0, 1),
                                   labels = c("Low Education", "High Education")))
d5 <- d4[!(is.na(d4$donatedummy)),]
educ_mm_ftest <- cj_anova(donatedummy ~  
                            `Nature of Violence` + Perpetrator + Crime +
                            `Victim Occupation` + `Victim Nationality` + 
                            `Victim Ethnicity` +
                            `Victim Gender` + `Respondent Ethnicity` +
                            `Respondent Gender`+ `Respondent SES`,
                          data = d5, id = ~ ticket, by = ~ Education)
educ_mm_ftest

# donatedummy
educ_mm_donatedummy <- cj(donatedummy ~ 
                            `Nature of Violence` + Perpetrator + Crime +
                            `Victim Occupation` + `Victim Nationality` + 
                            `Victim Ethnicity` +
                            `Victim Gender` + `Respondent Ethnicity`,
                          data = d5, id = ~ ticket, by = ~ Education, h0 = 0.5,
                          estimate = "mm")
plot(educ_mm_donatedummy, group="Education", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("educ_mm_donatedummy.pdf", width = 11, height = 12.5, dpi = 600)


# donate (amount)
educ_mm_donateamount <- cj(donateamount ~ 
                             `Nature of Violence` + Perpetrator + Crime +
                             `Victim Occupation` + `Victim Nationality` + 
                             `Victim Ethnicity` +
                             `Victim Gender` + `Respondent Ethnicity`,
                           data = d5, id = ~ ticket, by = ~ Education, h0=0.5,
                           estimate="mm")
plot(educ_mm_donateamount, group="Education", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("educ_mm_donateamount.pdf", width = 11, height = 12.5, dpi = 600)

# feeling therm
educ_therm_mm <- cj(therm ~ 
                      `Nature of Violence` + Perpetrator + Crime +
                      `Victim Occupation` + `Victim Nationality` + 
                      `Victim Ethnicity` +
                      `Victim Gender` + `Respondent Ethnicity`,
                    data = d5, id = ~ ticket, by = ~ Education, h0=0.5,
                    estimate="mm")
plot(educ_therm_mm, group="Education", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("educ_mm_therm.pdf", width = 11, height = 12.5, dpi = 600)


# social distance
educ_socdist_mm <- cj(socialdist ~ 
                        `Nature of Violence` + Perpetrator + Crime +
                        `Victim Occupation` + `Victim Nationality` + 
                        `Victim Ethnicity` +
                        `Victim Gender` + `Respondent Ethnicity`,
                      data = d5, id = ~ ticket, by = ~ Education, 
                      h0=0.5, estimate="mm")
plot(educ_socdist_mm, group="Education", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("educ_mm_socialdistance.pdf",
       width = 11, height = 12.5, dpi = 600)


# Empathy
educ_empathy_mm <- cj(empathy ~ 
                        `Nature of Violence` + Perpetrator + Crime +
                        `Victim Occupation` + `Victim Nationality` + 
                        `Victim Ethnicity` +
                        `Victim Gender` + `Respondent Ethnicity`,
                      data = d5, id = ~ ticket, by = ~ Education, 
                      h0=0.5, estimate="mm")
plot(educ_empathy_mm, group="Education", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("educ_mm_empathy.pdf", width = 11, height = 12.5, dpi = 600)

# Norms
educ_norms_mm <- cj(norms ~ 
                      `Nature of Violence` + Perpetrator + Crime +
                      `Victim Occupation` + `Victim Nationality` + 
                      `Victim Ethnicity` +
                      `Victim Gender` + `Respondent Ethnicity`,
                    data = d5, id = ~ ticket, by = ~ Education, 
                    h0=0.5, estimate="mm")
plot(educ_norms_mm, group = "Education", vline = .5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("educ_mm_norms.pdf", width = 11, height = 12.5, dpi = 600)

## By Interest in politics
d4 <- d4 %>%
  # educ = 7 is 'Preparatoria Terminada', so Education = 1 are those who are at
  # least ready to start college
  dplyr::mutate(Interest = ifelse(d4$interest >= 2, 1, 0),
                Interest = factor(Interest,
                                   levels = c(0, 1),
                                   labels = c("Low Interest", "High Interest")))
d5 <- d4[!(is.na(d4$donatedummy)),]
interest_mm_ftest <- cj_anova(donatedummy ~  
                            `Nature of Violence` + Perpetrator + Crime +
                            `Victim Occupation` + `Victim Nationality` + 
                            `Victim Ethnicity` +
                            `Victim Gender` + `Respondent Ethnicity` +
                            `Respondent Gender`+ `Respondent SES`,
                          data = d5, id = ~ ticket, by = ~ Interest)
interest_mm_ftest

# donatedummy
interest_mm_donatedummy <- cj(donatedummy ~ 
                            `Nature of Violence` + Perpetrator + Crime +
                            `Victim Occupation` + `Victim Nationality` + 
                            `Victim Ethnicity` +
                            `Victim Gender` + `Respondent Ethnicity`,
                          data = d5, id = ~ ticket, by = ~ Interest, h0 = 0.5,
                          estimate = "mm")
plot(interest_mm_donatedummy, group="Interest", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("interest_mm_donatedummy.pdf", width = 11, height = 12.5, dpi = 600)


# donate (amount)
interest_mm_donateamount <- cj(donateamount ~ 
                             `Nature of Violence` + Perpetrator + Crime +
                             `Victim Occupation` + `Victim Nationality` + 
                             `Victim Ethnicity` +
                             `Victim Gender` + `Respondent Ethnicity`,
                           data = d5, id = ~ ticket, by = ~ Interest, h0=0.5,
                           estimate="mm")
plot(interest_mm_donateamount, group="Interest", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("interest_mm_donateamount.pdf", width = 11, height = 12.5, dpi = 600)

# feeling therm
interest_therm_mm <- cj(therm ~ 
                      `Nature of Violence` + Perpetrator + Crime +
                      `Victim Occupation` + `Victim Nationality` + 
                      `Victim Ethnicity` +
                      `Victim Gender` + `Respondent Ethnicity`,
                    data = d5, id = ~ ticket, by = ~ Interest, h0=0.5,
                    estimate="mm")
plot(interest_therm_mm, group="Interest", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("interest_mm_therm.pdf", width = 11, height = 12.5, dpi = 600)


# social distance
interest_socdist_mm <- cj(socialdist ~ 
                        `Nature of Violence` + Perpetrator + Crime +
                        `Victim Occupation` + `Victim Nationality` + 
                        `Victim Ethnicity` +
                        `Victim Gender` + `Respondent Ethnicity`,
                      data = d5, id = ~ ticket, by = ~ Interest, 
                      h0=0.5, estimate="mm")
plot(interest_socdist_mm, group="Interest", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("interest_socdist_mm.pdf",
       width = 11, height = 12.5, dpi = 600)


# Empathy
interest_empathy_mm <- cj(empathy ~ 
                        `Nature of Violence` + Perpetrator + Crime +
                        `Victim Occupation` + `Victim Nationality` + 
                        `Victim Ethnicity` +
                        `Victim Gender` + `Respondent Ethnicity`,
                      data = d5, id = ~ ticket, by = ~ Interest, 
                      h0=0.5, estimate="mm")
plot(interest_empathy_mm, group="Interest", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("interest_empathy_mm.pdf", width = 11, height = 12.5, dpi = 600)

# Norms
educ_norms_mm <- cj(norms ~ 
                      `Nature of Violence` + Perpetrator + Crime +
                      `Victim Occupation` + `Victim Nationality` + 
                      `Victim Ethnicity` +
                      `Victim Gender` + `Respondent Ethnicity`,
                    data = d5, id = ~ ticket, by = ~ Interest, 
                    h0=0.5, estimate="mm")
plot(educ_norms_mm, group = "Interest", vline = .5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("interest_mm_norms.pdf", width = 11, height = 12.5, dpi = 600)



## By Amount of Talk About Politics
d4 <- d4 %>%
  # educ = 7 is 'Preparatoria Terminada', so Education = 1 are those who are at
  # least ready to start college
  dplyr::mutate(Talk = ifelse(d4$talk >= 2, 1, 0),
                Talk = factor(Talk,
                                  levels = c(0, 1),
                                  labels = c("Talked Little", "Talked a Lot")))
d5 <- d4
talk_mm_ftest <- cj_anova(donatedummy ~  
                                `Nature of Violence` + Perpetrator + Crime +
                                `Victim Occupation` + `Victim Nationality` + 
                                `Victim Ethnicity` +
                                `Victim Gender` + `Respondent Ethnicity` +
                                `Respondent Gender`+ `Respondent SES`,
                              data = d5, id = ~ ticket, by = ~ Talk)
talk_mm_ftest

# donatedummy
talk_mm_donatedummy <- cj(donatedummy ~ 
                                `Nature of Violence` + Perpetrator + Crime +
                                `Victim Occupation` + `Victim Nationality` + 
                                `Victim Ethnicity` +
                                `Victim Gender` + `Respondent Ethnicity`,
                              data = d5, id = ~ ticket, by = ~ Talk, h0 = 0.5,
                              estimate = "mm")
plot(talk_mm_donatedummy, group="Talk", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("talk_mm_donatedummy.pdf", width = 11, height = 12.5, dpi = 600)


# donate (amount)
talk_mm_donateamount <- cj(donateamount ~ 
                                 `Nature of Violence` + Perpetrator + Crime +
                                 `Victim Occupation` + `Victim Nationality` + 
                                 `Victim Ethnicity` +
                                 `Victim Gender` + `Respondent Ethnicity`,
                               data = d5, id = ~ ticket, by = ~ Talk, h0=0.5,
                               estimate="mm")
plot(talk_mm_donateamount, group="Talk", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("talk_mm_donateamount.pdf", width = 11, height = 12.5, dpi = 600)

# feeling therm
talk_therm_mm <- cj(therm ~ 
                          `Nature of Violence` + Perpetrator + Crime +
                          `Victim Occupation` + `Victim Nationality` + 
                          `Victim Ethnicity` +
                          `Victim Gender` + `Respondent Ethnicity`,
                        data = d5, id = ~ ticket, by = ~ Talk, h0=0.5,
                        estimate="mm")
plot(talk_therm_mm, group="Talk", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("talk_mm_therm.pdf", width = 11, height = 12.5, dpi = 600)


# social distance
talk_socdist_mm <- cj(socialdist ~ 
                            `Nature of Violence` + Perpetrator + Crime +
                            `Victim Occupation` + `Victim Nationality` + 
                            `Victim Ethnicity` +
                            `Victim Gender` + `Respondent Ethnicity`,
                          data = d5, id = ~ ticket, by = ~ Talk, 
                          h0=0.5, estimate="mm")
plot(talk_socdist_mm, group="Talk", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("talk_socdist_mm.pdf",
       width = 11, height = 12.5, dpi = 600)


# Empathy
talk_empathy_mm <- cj(empathy ~ 
                            `Nature of Violence` + Perpetrator + Crime +
                            `Victim Occupation` + `Victim Nationality` + 
                            `Victim Ethnicity` +
                            `Victim Gender` + `Respondent Ethnicity`,
                          data = d5, id = ~ ticket, by = ~ Talk, 
                          h0=0.5, estimate="mm")
plot(talk_empathy_mm, group="Talk", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("talk_empathy_mm.pdf", width = 11, height = 12.5, dpi = 600)

# Norms
educ_norms_mm <- cj(norms ~ 
                      `Nature of Violence` + Perpetrator + Crime +
                      `Victim Occupation` + `Victim Nationality` + 
                      `Victim Ethnicity` +
                      `Victim Gender` + `Respondent Ethnicity`,
                    data = d5, id = ~ ticket, by = ~ Talk, 
                    h0=0.5, estimate="mm")
plot(educ_norms_mm, group = "Talk", vline = .5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("talk_mm_norms.pdf", width = 11, height = 12.5, dpi = 600)


## By Gender
d5 <- d4

# donatedummy
talk_mm_donatedummy <- cj(donatedummy ~ 
                            `Nature of Violence` + Perpetrator + Crime +
                            `Victim Occupation` + `Victim Nationality` + 
                            `Victim Ethnicity` +
                            `Victim Gender` + `Respondent Ethnicity`,
                          data = d5, id = ~ ticket, by = ~ Gender, h0 = 0.5,
                          estimate = "mm")
plot(talk_mm_donatedummy, group="Gender", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("gender_mm_donatedummy.pdf", width = 11, height = 12.5, dpi = 600)


# donate (amount)
talk_mm_donateamount <- cj(donateamount ~ 
                             `Nature of Violence` + Perpetrator + Crime +
                             `Victim Occupation` + `Victim Nationality` + 
                             `Victim Ethnicity` +
                             `Victim Gender` + `Respondent Ethnicity`,
                           data = d5, id = ~ ticket, by = ~ Gender, h0=0.5,
                           estimate="mm")
plot(talk_mm_donateamount, group="Gender", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("gender_mm_donateamount.pdf", width = 11, height = 12.5, dpi = 600)

# feeling therm
talk_therm_mm <- cj(therm ~ 
                      `Nature of Violence` + Perpetrator + Crime +
                      `Victim Occupation` + `Victim Nationality` + 
                      `Victim Ethnicity` +
                      `Victim Gender` + `Respondent Ethnicity`,
                    data = d5, id = ~ ticket, by = ~ Gender, h0=0.5,
                    estimate="mm")
plot(talk_therm_mm, group="Gender", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("gender_mm_therm.pdf", width = 11, height = 12.5, dpi = 600)


# social distance
talk_socdist_mm <- cj(socialdist ~ 
                        `Nature of Violence` + Perpetrator + Crime +
                        `Victim Occupation` + `Victim Nationality` + 
                        `Victim Ethnicity` +
                        `Victim Gender` + `Respondent Ethnicity`,
                      data = d5, id = ~ ticket, by = ~ Gender, 
                      h0=0.5, estimate="mm")
plot(talk_socdist_mm, group="Gender", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("gender_socdist_mm.pdf",
       width = 11, height = 12.5, dpi = 600)


# Empathy
talk_empathy_mm <- cj(empathy ~ 
                        `Nature of Violence` + Perpetrator + Crime +
                        `Victim Occupation` + `Victim Nationality` + 
                        `Victim Ethnicity` +
                        `Victim Gender` + `Respondent Ethnicity`,
                      data = d5, id = ~ ticket, by = ~ Gender, 
                      h0=0.5, estimate="mm")
plot(talk_empathy_mm, group="Gender", vline=.5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("gender_empathy_mm.pdf", width = 11, height = 12.5, dpi = 600)

# Norms
educ_norms_mm <- cj(norms ~ 
                      `Nature of Violence` + Perpetrator + Crime +
                      `Victim Occupation` + `Victim Nationality` + 
                      `Victim Ethnicity` +
                      `Victim Gender` + `Respondent Ethnicity`,
                    data = d5, id = ~ ticket, by = ~ Gender, 
                    h0=0.5, estimate="mm")
plot(educ_norms_mm, group = "Gender", vline = .5, size = 3) +
  theme_bw() +
  theme(legend.position="bottom",
        axis.text.y = element_text(size = 16),
        axis.text.x = element_text(size = 16),
        axis.title.x = element_text(size = 16),
        legend.title = element_text(size=16),
        legend.text = element_text(size=14))
ggsave("Gender_mm_norms.pdf", width = 11, height = 12.5, dpi = 600)



detach("package:cregg", unload=TRUE)

#### Other subgroup analysis pre-registered -----
d5 <- d4[!(is.na(d4$donatedummy)),]
## By SES
d5 <- d4[!(is.na(d4$`Respondent SES`)),]
m = amce(donatedummy ~  `Nature of Violence` * `Respondent SES` + 
           Perpetrator + Crime + `Victim Occupation` + `Victim Nationality` +
           `Victim Ethnicity` + `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`,
         data = d5, respondent.id = "resp.id"); summary(m)

## By ethnicity
m = amce(donatedummy ~  `Nature of Violence` * `Respondent Ethnicity` + 
           Perpetrator + Crime + `Victim Occupation` + `Victim Nationality` +
           `Victim Ethnicity` + `Victim Gender` +
           `Respondent SES` +  `Respondent Gender`,
         data = d5, respondent.id = "resp.id"); summary(m)
## By Party ID
# create dummy for Morena supporters
d5 <- d5 %>%
  mutate(Morena = ifelse(party == 8, 1, 0)) %>%
  filter(!is.na(Morena))
m = amce(donatedummy ~  `Nature of Violence`*Morena + `Respondent Ethnicity` + 
           Perpetrator + Crime + `Victim Occupation` + `Victim Nationality` +
           `Victim Ethnicity` + `Victim Gender` +
           `Respondent SES` +  `Respondent Gender`,
         data = d5, respondent.id = "resp.id"); summary(m)

###### Average Component Interaction Effects (ACIE):
###### Interactions between controllability and perpetrator
## Donate dummy
d5 <- d4[!(is.na(d4$donatedummy)),]
d5 <- d5 %>%
  dplyr::filter(!is.na(`Respondent Ethnicity`))
d5$donatedummy = as.numeric(d5$donatedummy)
m = amce(donatedummy ~  `Nature of Violence` * Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot.amce(m, ci=.95, xlab="Change in Pr(Donating)",
          text.size=16, point.size = 1)
ggsave(file="fig_donatedummy_interaction_typeViolXPerpet.pdf",
       width = 11, height = 12.5, dpi = 600)

## Donate amount
d5 <- d4[!(is.na(d4$donateamount)),]
d5 <- d4[!(is.na(d4$`Respondent Ethnicity`)),]
m = amce(donateamount ~  `Nature of Violence` * Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "ticket")
plot.amce(m, ci=.95, xlab="Change in Amount Donated", text.size=16,
          point.size = 1)
ggsave(file="fig_donateamount_interaction_typeViolXPerpet.pdf",
       width=11, height=12.5, dpi=600)

## Feeling thermometer
d5 <- d4[!(is.na(d4$therm)),]
d5 <- d4[!(is.na(d4$`Respondent Ethnicity`)),]
m = amce(therm ~  `Nature of Violence` * Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot.amce(m, ci=.95,
          xlab="Change in Feelings Toward Victim (Feeling Thermometer)",
          text.size=16, point.size = 1)
ggsave(file="fig_therm_interaction_typeViolXPerpet.pdf",
       width=11, height=12.5, dpi=600)

# Social Distance
d5 <- d4[!(is.na(d4$socialdist)),]
d5 <- d4 %>%
  dplyr::filter(!is.na(socialdist), !is.na(`Respondent Ethnicity`))
m = amce(socialdist ~  `Nature of Violence` * Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot(m, ci=.95, xlab="Change in Social Distance Tolerance with Victims",
     text.size=16)
ggsave(file="fig_socialdist_typeViolXPerpet.pdf",
       width=11, height=12.5, dpi=600)


####### Mechanisms as DV
# Empathy/Compassion
d5 <- d4 %>%
  dplyr::filter(!is.na(empathy), !is.na(`Respondent Ethnicity`))
m = amce(empathy ~  `Nature of Violence` + Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot.amce(m, ci=.95, xlab="Change in Compassion for Victims",
          text.size=16, point.size = 1,
          group.order = c("Nature of Violence", "Crime", "Perpetrator",
                          "Victim Occupation",  "Victim Nationality",
                          "Victim Ethnicity", "Victim Gender",
                          "Respondent Ethnicity", "Respondent Gender",
                          "Respondent SES"))
ggsave(file="fig_empathy.pdf", width=11, height=12.5, dpi=600)


# Social Norms
d5 <- d4 %>%
  dplyr::filter(!is.na(norms), !is.na(`Respondent Ethnicity`))
m = amce(norms ~  `Nature of Violence` + Perpetrator + Crime +
           `Victim Occupation` + `Victim Nationality` + `Victim Ethnicity` +
           `Victim Gender` +
           `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
         data = d5, respondent.id = "resp.id")
plot(m, ci=.95, xlab="Change in Social Norms about Helping Victims",
     text.size=16, point.size = 1,
     group.order = c("Nature of Violence", "Crime", "Perpetrator",
                     "Victim Occupation",  "Victim Nationality",
                     "Victim Ethnicity", "Victim Gender",
                     "Respondent Ethnicity", "Respondent Gender",
                     "Respondent SES"))

## Correlated of solidarity
library(estimatr)
m <- lm_robust(donatedummy ~  SELs + Gender + Ages + educ + presidapproval +
                 ideology_1 + empathy_baseline_2 + trustffaa + 
                 identif_victims_4 + `Indirect Victim` + factor(party) + ethnic,
               data = d5, clusters = resp.id)
modelsummary(m, stars = T)


################### Descriptive Stats --------

###### Descriptives Study 1
d2 <- readRDS("Study1.rds")
d2$`Letter Writing` <- as.numeric(d2$`Letter Writing`)
d2$`Exposure to Victimization` <- as.numeric(d2$`Exposure to Victimization`)

descript.Study1 = d2 %>%
  dplyr::select("Letter Writing", "Attributional Thinking",
                `Feeling Thermometer (Disappeared, Unstandarized)`,
                "Social Distance Index",
                Empathy,
                Norms, everything(),
                -"Anxiety", -"Victim Blaming Index",-Region,-Party,
                -"Feeling Thermometer (Migrants, Unstandarized)",
                -"Identification with victims (Self-Other Merging)") %>%
  rename(Compassion = Empathy,
         "Controllability" = "Attributional Thinking",
       #  Age=Ages,
         `Feeling Thermometer toward the Disappeared` = 
           `Feeling Thermometer (Disappeared, Unstandarized)`)

datasummary(`Letter Writing`+`Controllability`+`Feeling Thermometer toward the Disappeared`+`Social Distance Index`+`Compassion`+`Norms`+`Ideology`+`Baseline Empathy`+`Ages`+`Socioeconomic Status`+`Exposure to Victimization`+`Trust in Armed Forces`+`Identification with organized victims`~ N+ Mean + SD + Min + Median + Max, data = descript.Study1,
            output = "latex",
            title = "Descriptive Statistics (Study 1)")

###### Descriptives Study 2
descript.Study2 = d4 %>%
  dplyr::select(donatedummy:"Respondent SES", -resp.id, -Muni, -State, -ethnic,
                -presidapproval,-interest,-talk,-party,
                -empathy_baseline_4, -GEO, -trustffaa) %>%
  rename("Donate (Dummy)" = donatedummy,
         "Donate (Amount)" = donateamount,
         "Feeling Thermometer" = therm,
         "Social Distance" = socialdist,
         "Compassion for Victims" = empathy,
         "Norms Rating" = norms,
         "Socio-Economic Level" = SELs,
         "Respondent Age" = Ages,
         "Education Level" = educ,
         "Ideology" = ideology_1,
         "Empathy Baseline" = "empathy_baseline_2",
         "Trust in Armed Forces" = "trust_ffaa_1",
         "Identification with Victims" = "identif_victims_4",
  )

datasummary(`Donate (Dummy)`+`Donate (Amount)`+`Feeling Thermometer`+`Social Distance`+`Compassion for Victims`+`Norms Rating`+`Socio-Economic Level`+as.numeric(Gender)+`Respondent Age`+`Education Level`+`Ideology`+`Empathy Baseline`+`Trust in Armed Forces`+`Identification with Victims`+`Indirect Victim`~ N+ Mean + SD + Min + Median + Max, data = descript.Study2,
            output = "latex",
            title = "Descriptive Statistics (Study 2)")

####### Balance tables  --------

# Gender
d5 <- d4[!(is.na(d4$Gender)),]
d5$Gender <- as.numeric(d5$Gender)
balance_gender = lm_robust(Gender ~  `Nature of Violence` + Perpetrator +
                        Crime +  `Victim Occupation` + `Victim Nationality` +
                        `Victim Ethnicity` +  `Victim Gender` +
         `Respondent Ethnicity` +  `Respondent Gender`+ `Respondent SES`,
       data = d5, cluster = resp.id); summary(balance_gender)

# Age
d5 <- d4[!(is.na(d4$Ages)),]
d5$Gender <- as.numeric(d5$Ages)
balance_Age = lm_robust(Ages ~  `Nature of Violence` + Perpetrator +
                        Crime +  `Victim Occupation` + `Victim Nationality` +
                        `Victim Ethnicity` +  `Victim Gender` +
                        `Respondent Ethnicity` +  `Respondent Gender`+ 
                     `Respondent SES`,
                      data = d5, cluster = resp.id); summary(balance_Age)

# Educ
d5 <- d4[!(is.na(d4$Education)),]
d5$Education <- as.numeric(d5$Education)
balance_Educ = lm_robust(Education ~  `Nature of Violence` + Perpetrator +
                     Crime +  `Victim Occupation` + `Victim Nationality` +
                     `Victim Ethnicity` +  `Victim Gender` +
                     `Respondent Ethnicity` +  `Respondent Gender`+ 
                     `Respondent SES`,
                   data = d5, cluster = resp.id); summary(balance_Educ)


# SES
d5 <- d4[!(is.na(d4$SELs)),]
d5$SELs <- as.numeric(d5$SELs)
balance_SES = lm_robust(SELs ~  `Nature of Violence` + Perpetrator +
                      Crime +  `Victim Occupation` + `Victim Nationality` +
                      `Victim Ethnicity` +  `Victim Gender` +
                      `Respondent Ethnicity` +  `Respondent Gender`+ 
                      `Respondent SES`,
                    data = d5, cluster = resp.id); summary(balance_SES)


# Ideology
d5 <- d4[!(is.na(d4$Ideology)),]
d5$Ideology <- as.numeric(d5$Ideology)
balance_Ideo = lm_robust(Ideology ~  `Nature of Violence` + Perpetrator +
                      Crime +  `Victim Occupation` + `Victim Nationality` +
                      `Victim Ethnicity` +  `Victim Gender` +
                      `Respondent Ethnicity` +  `Respondent Gender`+ 
                      `Respondent SES`,
                    data = d5, cluster = resp.id); summary(balance_Ideo)



# Trust in FFAA
d5 <- d4[!(is.na(d4$trust_ffaa_1)),]
d5$trust_ffaa_1 <- as.numeric(d5$trust_ffaa_1)
balance_FFAA_Trust = lm_robust(trust_ffaa_1 ~  `Nature of Violence` + Perpetrator +
                      Crime +  `Victim Occupation` + `Victim Nationality` +
                      `Victim Ethnicity` +  `Victim Gender` +
                      `Respondent Ethnicity` +  `Respondent Gender`+ 
                      `Respondent SES`, data = d5,
                      cluster = resp.id); summary(balance_FFAA_Trust)

## export table
balance_models <- list(
  "Gender" = balance_gender,
  "Age" = balance_Age,
  "Education (Low/High)" = balance_Educ,
  "Socioeconomic Status" = balance_SES
)


msummary(balance_models, stars = T, coef_omit = "Intercept",
         coef_rename = c("`Nature of Violence`Indiscriminate" = "Indiscriminate Violence",
                         "PerpetratorNarcos" = "Peroetrator: Narcos",
                         "PerpetratorPolice" = "Perpetrator: Police",
                         "CrimeDisplaced" = "Crime: Displacement",
                         "CrimeExtorsion" = "Crime: Extortion",
                         "CrimeMurder" = "Crime: Murder",
                         "CrimeTorture" = "Crime: Torture",
                         "`Victim Occupation`Street Vendor" = "Victim Occupation: Street Vendor",
                         "`Victim Nationality`Mexican" = "Victim Nationality: Mexican",
                         "`Victim Ethnicity`Mixed" = "Victim Ethnicity: Mixed",
                         "`Victim Ethnicity`White" = "Victim Ethnicity: White",
                         "`Victim Gender`Male" = "Victim Gender: Male",
                         "`Respondent Ethnicity`Same as Victim" =
                           "Respondent Ethnicity: Same as Victim",
                         "`Respondent Gender`Same as Victim" = 
                           "Respondent Gender: Same as Victim",
                         "`Respondent SES`Same as Victim" = 
                           "Respondent SES: Same as Victim"),
         title = "Balance Table---Factorial Experiment Randomization Check",
         output = "latex",
         notes = "Standard error clustered by respondent.")

